window.$config = {
    sreen : 500,
    type : false
}

var app = angular.module('webApp', ['ngRoute','iService','iDirective','Filter','angularCSS']);
// 页面路由
app.config(['$controllerProvider', '$compileProvider', '$filterProvider', '$provide', '$routeProvider','$locationProvider','$cssProvider',function($controllerProvider, $compileProvider, $filterProvider, $provide, $routeProvider,$locationProvider,$cssProvider) {
    app.register = {
        controller: $controllerProvider.register,
        directive: $compileProvider.directive,
        filter: $filterProvider.register,
        factory: $provide.factory,
        service: $provide.service
    };
    app.asyncjs = function (js) {
        return ["$q", "$route", "$rootScope", function ($q, $route, $rootScope) {
            var deferred = $q.defer();
            var dependencies = js;
            $script(dependencies, function () {
                $rootScope.$apply(function () {
                    deferred.resolve();
                });
            });
            return deferred.promise;
        }];
    }
    angular.extend($cssProvider.defaults, {
        persist: true
    });
    //动态路由
    $routeProvider
    .when('/', {    //首页
    templateUrl: 'views/home.html',
    controller:'homeCtrl',
    css:__uri('../css/home.less')
    /*resolve: {
        load: app.asyncjs([__uri('haha.js')])
    }*/
    }).when('/list/:section/:type', {    //首页
        templateUrl: 'views/list.html',
        controller:'listCtrl',
        css:__uri('../css/list.less')
    }).when('/detail', {    //首页
        templateUrl: 'views/detail.html',
        controller:'detailCtrl',
    }).otherwise({
        redirectTo : '/'
    });
    // $locationProvider.html5Mode(true);
    $locationProvider.hashPrefix('');
}]);


// 在所有route跳转前进行登录检查，目标页面需要登录且用户未登录，则跳转到登录页面
app.run(['$rootScope', function($rootScope) {
    $rootScope.$on('$routeChangeStart', function(evt, next, current) {
        // 如果用户未登录
        /*if(next.templateUrl=='partials/donateRank.html'){
            var page = 'donate';
        }else if(next.templateUrl=='partials/shareRank.html'){
            var page = 'share';
        }*/
    })
}]);



app.controller('appCtrl',['$scope','$rootScope','$location','$http','hostUrl', function ($scope,$rootScope,$location,$http,hostUrl) {
    // 页面加载完成
    $scope.$on('$viewContentLoaded', function() {
        pcMobileLoad();
    });
    $(window).resize(function () {
        pcMobileLoad();
    });

    $scope.goUrl = function (url) {
        $location.path(url);
    }

    //手机和pc重新加载
    function pcMobileLoad() {
        var winW = $(window).width();
        if(!$config.type){
            if(winW>$config.sreen){
                $config.type = 'pc';
            }else{
                $config.type = 'mobile';
            }
            console.log($config.type)
        }else{
            if(($config.type == 'pc' && winW<=$config.sreen) || ($config.type == 'mobile' && winW > $config.sreen)){
                location.reload();
            }
        }
    }

    //设置title
    $scope.update_title = function(title) {
        var body = document.getElementsByTagName('body')[0];
        document.title = title;
        var iframe = document.createElement("iframe");
        iframe.setAttribute("src", "/empty.png");

        iframe.addEventListener('load', function() {
            setTimeout(function() {
                iframe.removeEventListener('load');
                document.body.removeChild(iframe);
            }, 0);
        });
        document.body.appendChild(iframe);
    }
}]);